package com.example.logindb.controller;



import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.entity.Help;
import com.example.logindb.entity.Location;
import com.example.logindb.entity.Online;
import com.example.logindb.entity.User;
import com.example.logindb.repository.LocationRepository;
import com.example.logindb.sevices.HelpService;
import com.example.logindb.sevices.OnlineService;
import com.example.logindb.sevices.UserService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/online")
public class OnlineController {

    private final LocationRepository locationRepository;
    private OnlineService onlineService;
    private UserService userService;
    private HelpService helpService;
    @Autowired
    public OnlineController(OnlineService onlineService, UserService userService, HelpService helpService, LocationRepository locationRepository) {
        this.onlineService = onlineService;
        this.userService = userService;
        this.helpService = helpService;
        this.locationRepository = locationRepository;
    }
    //更新在线状态
    @PostMapping
    public ResponseMessage<Online> updateStatus(@RequestBody Online online)
    {

        onlineService.updateStatus(online);
        //查询是否为盲人离线的状态,将无人接听的求助信息设为超时
        if(online.getStatus()==0){
            User user = userService.findAllById(online.getId());
            if(!Objects.equals(user.getUserrole(), "blind")){
                List<Help> helps=helpService.findByBid(online.getId());
                for (Help value : helps) {
                    if (value.getStatus() == 0) {
                        value.setStatus(3);
                        helpService.endblind(value);
                    }
                }
            }
        }

        return ResponseMessage.success(online);
    }
    //志愿者查询自己在线状态以及是否有正在帮助的盲人
    @GetMapping("/volunteer")
    public BlindStatus findBlindStstusAndHelping(@RequestParam int vid)
    {
        //查志愿者在线状态
        Online online = onlineService.findStatusById(vid);
        BlindStatus bs = new BlindStatus();
        //返回status
        bs.setStatus(online.getStatus());
        //找到志愿者id对应status为1的求助信息
        Help help = helpService.findStatus1AllByBid(vid);
        if(help!=null)
        {
            Location location = locationRepository.findAllByLocid(vid);
            bs.setPhone(userService.findAllById(help.getBid()).getUsername());
            bs.setDistance(helpService.getDistance(help.getLon(),help.getLat(),location.getLon(),location.getLat()));
        }
        bs.setHelp(help);
        return bs;


    }

    //查询在线状态
    @GetMapping("/{id}")
    public ResponseMessage<Online> get_relation(@PathVariable Integer id)
    {
        return ResponseMessage.success(onlineService.findStatusById(id));
    }


    @Setter
    @Getter
    static class BlindStatus{
        private Integer status;
        private Double distance;
        private Help help;
        public String phone;
    }



}
